﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
using StuManDAL.Common;
using StuManModels;

namespace StuManDAL
{
    public class SubjectService
    {
        public List<StudentSubject> GetAllSubjects(out string info)
        {
            string sql = "select SubjectId,SubjectName from StudentSubject";
            List<StudentSubject> subjects = new List<StudentSubject>();
            SqlDataReader reader=null;
            try
            {
                 reader = SQLHelper.Instance.GetReader(sql);
                while (reader.Read())
                {
                    subjects.Add(new StudentSubject()
                    {
                        SubjectId = Convert.ToInt32(reader["SubjectId"]),
                        SubjectName = reader["SubjectName"].ToString()
                    });
                }
                info = string.Empty;
                return subjects;
            }
            catch (SqlException ex)
            {
                info = "操作数据库出现异常，异常信息为：" + ex.Message;
                return null;
            }
            catch (Exception ex)
            {
                info = ex.Message;
                return null;
            }
            finally
            {
                reader.Close();
            }
        }
    }
}
